package com.watf.functions.core;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import com.thoughtworks.selenium.Selenium;
import com.watf.functions.core.DefaultBaseTestCase;


public class TransferTestCases extends DefaultBaseTestCase{
	/*
	 * Common variables
	 */
	public static Selenium selenium;
	
	//The files you want to transfer location
	public static String FilesPath="C:\\Users\\Jester\\Desktop\\123\\";
	
	//The test cases location you want to put.
	public static String TestCasesLocation="C:\\Users\\Jester\\Desktop\\eclipse\\Projects\\WATF\\src\\com\\watf\\testcases\\";
	
	//The Page definition location you want to put.
	public static String PagesLocation="C:\\Users\\Jester\\Desktop\\eclipse\\Projects\\WATF\\src\\com\\watf\\pages\\";
	
	//The page package information. 
	public static String PagesPackage="com.watf.pages";
	
	//The Test Data location information.
	public static String TestDataLocation="C:\\Users\\Jester\\Desktop\\eclipse\\Projects\\WATF\\src\\com\\watf\\testdata\\";
	
	public static void main(String[] args) throws FileNotFoundException
	{
		String ipath=FilesPath;

		ipath=ipath.replace("/", File.separator);
		ipath=ipath.replace("\\", File.separator);
		
		
		File[] iTestFiles=getFileList(ipath);
		
		for (int i=0;i<iTestFiles.length; i++)
		{
			if (!iTestFiles[i].getName().startsWith("."))
			{				
				System.out.println(iTestFiles[i]);
				transferTestCase(iTestFiles[i]);
			}
		}
		
		System.out.println("The transfer has finished. Please verify it. :)");

	}
	
	public static void transferTestCase(File iTestFile) throws FileNotFoundException
	{
		String iFileName=iTestFile.getName().replace(".txt", "");
		
		String iPackage="com.watf.testcases";
		File iTestCase=new File(TestCasesLocation+iFileName+".java");
		
		try
		{			
			FileReader iFile=new FileReader(iTestFile);
			BufferedReader iBuff=new BufferedReader(iFile);
			
			FileWriter iFileW=new FileWriter(iTestCase);
			BufferedWriter iBuffW=new BufferedWriter(iFileW);
			
			File iPage=new File(PagesLocation+iFileName+"1.java");
			
			FileWriter iFileWPage=new FileWriter(iPage);
			BufferedWriter iBuffWPage=new BufferedWriter(iFileWPage);			
			
			
			File PropertyName=new File(TestDataLocation+iFileName+".properties");
			
			FileWriter iFileWPro=new FileWriter(PropertyName);
			BufferedWriter iBuffWPro=new BufferedWriter(iFileWPro);
			
			iBuffWPage.write("package com.watf.pages;\n\n"+"public class "+iFileName+"1\n{\n");
			iBuffWPage.flush();
			
			String iline=null;
			while ((iline=iBuff.readLine())!=null)
			{
				if (iline.contains("package "))
				{
					iline="package "+iPackage+";";
					iline=iline+"\n\n"+"import com.watf.functions.core.DefaultBaseTestCase;\nimport com.watf.functions.core.DefaultConfigUtil;\nimport com.watf.functions.gs.CommonFuncs;\nimport "+PagesPackage+"."+iFileName+"1;";
				}
				else if (iline.contains("org.testng.Assert") ||iline.contains("ava.util.regex.Pattern"))
				{
					continue;
				}
				else if (iline.contains("public class"))
				{
					iline=iline.replace("SeleneseTestNgHelper", "DefaultBaseTestCase")+"\n"+"	private Selenium selenium;";
				}
				else if(iline.trim().startsWith("selenium."))
				{
					iline=restructureIt(iline,iFileName,iBuffWPage,iBuffWPro);
				}
				
				
				
				iBuffW.write(iline);
				iBuffW.newLine();
				iBuffW.flush();
			}
			
			iBuffWPage.write("}");
			iBuffWPage.flush();
			
			iFile.close();
			iBuff.close();
			iFileW.close();
			iBuffW.close();
			iFileWPro.close();
			iBuffWPro.close();
			iFileWPage.close();
			iBuffWPage.close();
		}
		catch (Exception e)
		{
			System.out.println("The iTestCase "+iTestFile+" failed!");
			e.printStackTrace();
		}
		
	}
	
	public static File[] getFileList(String iFileFolder)
	{
		File iPath=new File(iFileFolder);

		
		if (iPath.isDirectory())
		{	
			return iPath.listFiles();
			
		}
		else
		{
			System.out.println("The folder "+iFileFolder+" is not a valued path! please verify it first!");
		}
		return null;
		
	}
	

	public static String restructureIt(String iline,String iTestCaseName,BufferedWriter iFileWPage,BufferedWriter iFileWPro) throws IOException
	{
		String iCommand=iline.trim().split("\\.")[0];
		iCommand=iline.replace(iCommand+".", "");
		
		String iParam=iCommand.split("\\(")[1].trim();
		String iAction=iCommand.split("\\(")[0].trim();
		String[] iParams=iParam.substring(0, iParam.length()-2).split(",");
	    
		String iLocator1 = null,iLocator2 = null;
		String iLocator1R = null,iLocator2R = null;
		
		if (iAction.equals("open")||iAction.equals("type")||iAction.equals("click")||iAction.equals("select"))
		{
		    iLocator1=iParams[0];
		    if (iParams.length>1)
		    {	    	
		    	iLocator2=iParams[1];
		    	iLocator2R=writeToProperties(iLocator2,iTestCaseName,iFileWPro);
		    }
			
		    iLocator1R = writeToPage(iLocator1,iTestCaseName,iFileWPage);
		}
		else if(iAction.equals("waitForPageToLoad"))
		{
			iLocator1=iParams[0];
			iLocator1R=writeToProperties(iLocator1,iTestCaseName,iFileWPro);
		}
		
		iline=iline.replace(iLocator1, iLocator1R);
		if (iLocator2R!=null)
		{			
			iline=iline.replace(iLocator2, iLocator2R);
		}

	    return iline;

		
	}
	
	
	public static String writeToPage(String iString,String iPageName,BufferedWriter iBuffW) throws IOException
	{		
		String iIdName=null;
		if ((iIdName=isTheValueExists(iString,PagesLocation+iPageName+"1.java")) == null)
		{
			iIdName=randomString(4);
			iBuffW.write("    public static final String "+iIdName+" = "+iString+";");
			iBuffW.newLine();
			iBuffW.flush();			
		}
					
	    return iPageName+"1."+iIdName;

		
	}
	
	public static String writeToProperties(String iString,String iPropertyName,BufferedWriter iBuffW) throws IOException
	{
		
		if (iString.equals("\"30000\""))
		{
			return "CommonFuncs.SHORT_WAIT";
		}
		String iIdName=null;
		
		if ((iIdName=isTheValueExists(iString,TestDataLocation+iPropertyName+".properties"))==null)
		{		
			iIdName=randomString(4);
			System.out.println(iString);
			iString.replace("\"", "");
			System.out.println(iString);
			iBuffW.write(iIdName+" = "+iString);
			iBuffW.newLine();
			iBuffW.flush();
		}
		
		return "DefaultConfigUtil.getDataInCase(getClassName(), \""+iIdName+"\")";
		
	}
	
	public static String isTheValueExists(String iValue,String iSearchFile) throws IOException
	{
		File iFile=new File(iSearchFile);
		
		if (iFile.exists())
		{		
			FileReader iFileS=new FileReader(iSearchFile);
			BufferedReader iBuffS=new BufferedReader(iFileS);
			String iline=null;
			while ((iline=iBuffS.readLine())!=null)
			{
				
				if (iline.contains(" = ")&&iline.split(" = ")[1].contains(iValue))
				{
					iFileS.close();
					iBuffS.close();
					return iline.split(" = ")[0].replace("public static final String", "").trim();
				}
			}
			
			iFileS.close();
			iBuffS.close();
		}
		return null;
		
	}
	
	
	/**
	   * Generate the random String. 
	   * */
	private static Random randGen = null;
	private static char[] numbersAndLetters = null;

	public static final String randomString(int length) {
	         if (length < 1) {
	             return null;
	         }
	         if (randGen == null) {
	                randGen = new Random();
	                numbersAndLetters = ("abcdefghijklmnopqrstuvwxyz" +
	                   "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	                 }
	         char [] randBuffer = new char[length];
	         for (int i=0; i<randBuffer.length; i++) {
	             randBuffer[i] = numbersAndLetters[randGen.nextInt(51)];
	         }
	         return new String(randBuffer);
	}
	
	
	/*	
	public static void executeTheCommand(String iCommand)
	{
		String iSeleniumCommand=iCommand.split("\\(")[0].trim();
		String iParam=iCommand.split("\\(")[1].trim();
		iParam=iParam.substring(0,iParam.length()-2);
		
		String[] iParams=iParam.split("\\,");
		
		if (iSeleniumCommand.equals("open"))
		{
			if (iParams[0]!=null && iParams.length>1)
			{				
				selenium.open(iParams[0],iParams[1]);
			}
			else
			{
				selenium.open(iParams[0]);
			}
		}
		else if(iSeleniumCommand.equals("type"))
		{
			selenium.type(iParams[0],iParams[1]);
		}
		else if(iSeleniumCommand.equals("click"))
		{
				selenium.click(iParams[0]);		
		}
		else if(iSeleniumCommand.equals("waitForPageToLoad"))
		{
			selenium.waitForPageToLoad(iParams[0]);
		}
		
	}*/

}
